# -*- coding: utf-8 -*-

from odoo import api, models
from odoo.exceptions import ValidationError


class DecimalPrecision(models.Model):
    _inherit = 'decimal.precision'

    @api.constrains('digits')
    def _check_main_currency_rounding(self):
        for precision in self:
            if precision.name != 'Justdoo.Product.UOM':
                continue
            rounding = 1.0 / 10.0 ** precision.digits
            dangerous_uom_list = self.env['justdoo.uom.uom'].search([('rounding', '<', rounding)])
            if not dangerous_uom_list or len(dangerous_uom_list) <= 0:
                continue
            uom_descriptions = [
                " - %s (id=%s, precision=%s)" % (uom.name, uom.id, uom.rounding) for uom in dangerous_uom_list
            ]
            uom_descriptions_str = '\n'.join(uom_descriptions)
            raise ValidationError('你设置产品单位精度小于以下单位：\n %s \n 请根据情况修改相应精度' % uom_descriptions_str)
        return True
